//
//  SXTLandingInPutView.m
//  SXT
//
//  Created by 赵金鹏 on 16/8/12.
//  Copyright © 2016年 赵金鹏. All rights reserved.
//

#import "SXTLandingInPutView.h"

@interface SXTLandingInPutView()<UITextFieldDelegate>

@property (strong, nonatomic)   UILabel *tostLabel;              /** 顶部提示输入手机号label */
@property (strong, nonatomic)   UITextField *userPhoneText;              /** 用户输入手机号的text */
@property (strong, nonatomic)   UITextField *passwordText;              /** 用户输入密码的text */
@property (strong, nonatomic)   UIButton *nextViewBtn;              /** 下一步按钮 */
@property (strong, nonatomic)   UIButton *goLoginBtn;              /** 去登陆按钮 */
@property (strong, nonatomic)   UILabel *whiteBackLabe;              /** 白色背景 */
@property (strong, nonatomic)   UILabel *grayLineLabel;              /** 灰色分割线 */


@end

@implementation SXTLandingInPutView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.tostLabel];
        [self addSubview:self.whiteBackLabe];
        [self addSubview:self.userPhoneText];
        [self addSubview:self.passwordText];
        [self addSubview:self.grayLineLabel];
        [self addSubview:self.nextViewBtn];
        [self addSubview:self.goLoginBtn];
    }
    return self;
}


- (void)layoutSubviews{
    [super layoutSubviews];
    __weak typeof (self) weakSelf = self;
    [_tostLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.mas_top);
        make.left.equalTo(weakSelf.mas_left).offset(15);
        make.height.mas_equalTo(35);
        make.right.equalTo(weakSelf.mas_right);
    }];
    
    [_whiteBackLabe mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.tostLabel.mas_bottom);
        make.left.equalTo(weakSelf.mas_left).offset(-1);
        make.right.equalTo(weakSelf.mas_right).offset(1);
        make.height.equalTo(@89);
    }];
    
    [_userPhoneText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.mas_left).offset(15);
        make.top.equalTo(weakSelf.whiteBackLabe.mas_top);
        make.size.mas_equalTo(CGSizeMake(VIEW_WIDTH, 44));
    }];
    
    [_passwordText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.mas_left).offset(15);
        make.top.equalTo(weakSelf.userPhoneText.mas_bottom).offset(1);
        make.size.mas_equalTo(CGSizeMake(VIEW_WIDTH, 44));
    }];
    
    [_grayLineLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@1);
        make.left.equalTo(weakSelf.mas_left).offset(15);
        make.right.equalTo(weakSelf.mas_right).offset(-15);
        make.top.equalTo(weakSelf.userPhoneText.mas_bottom);
    }];
    
    [_nextViewBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.passwordText.mas_bottom).offset(15);
        make.left.equalTo(weakSelf.mas_left).offset(16);
        make.height.equalTo(@35);
        make.right.equalTo(weakSelf.mas_right).offset(-15);
    }];
    
    [_goLoginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.nextViewBtn.mas_bottom).offset(23);
        make.right.equalTo(weakSelf.mas_right).offset(-16);
        make.size.mas_equalTo(CGSizeMake(70, 30));
    }];
}

- (UILabel *)tostLabel{
    if (!_tostLabel) {
        _tostLabel = [[UILabel alloc]init];
        _tostLabel.text = @"请输入手机号码注册新用户";
        _tostLabel.textColor = RGB(81, 81, 81);
        _tostLabel.font = [UIFont systemFontOfSize:14.0];
    }
    return _tostLabel;
}

- (UILabel *)whiteBackLabe{
    if (!_whiteBackLabe) {
        _whiteBackLabe = [[UILabel alloc]init];
        _whiteBackLabe.backgroundColor = [UIColor whiteColor];
        _whiteBackLabe.layer.borderColor = RGB(188, 186, 192).CGColor;
        _whiteBackLabe.layer.borderWidth = 1.0;
    }
    return _whiteBackLabe;
}

- (UILabel *)grayLineLabel{
    if (!_grayLineLabel) {
        _grayLineLabel = [[UILabel alloc]init];
        _grayLineLabel.backgroundColor = RGB(188, 186, 192);
    }
    return _grayLineLabel;
}
- (UITextField *)userPhoneText{
    if (!_userPhoneText) {
        _userPhoneText = [[UITextField alloc]init];
        _userPhoneText.delegate = self;
        _userPhoneText.placeholder = @"请输入手机号码";
        _userPhoneText.keyboardType = UIKeyboardTypeNumberPad;
        [_userPhoneText addTarget:self action:@selector(userTextFieldChangeText:) forControlEvents:(UIControlEventAllEditingEvents)];
    }
    return _userPhoneText;
}

- (UITextField *)passwordText{
    if (!_passwordText) {
        _passwordText = [[UITextField alloc]init];
        _passwordText.delegate = self;
        _passwordText.secureTextEntry = YES;
        _passwordText.placeholder = @"请输入密码";
        [_passwordText addTarget:self action:@selector(passwordTextChangeText:) forControlEvents:(UIControlEventAllEditingEvents)];
    }
    return _passwordText;
}

- (UIButton *)nextViewBtn{
    if (!_nextViewBtn) {
        _nextViewBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        _nextViewBtn.backgroundColor = RGB(229, 229, 229);
        _nextViewBtn.layer.masksToBounds = YES;
        _nextViewBtn.layer.cornerRadius = 5.0;
        _nextViewBtn.userInteractionEnabled = NO;
        [_nextViewBtn setTitle:@"下一步" forState:(UIControlStateNormal)];
        [_nextViewBtn addTarget:self action:@selector(goNextViewController) forControlEvents:(UIControlEventTouchUpInside)];
        _nextViewBtn.titleLabel.textColor = RGB(132, 132, 132);
        
    }
    return _nextViewBtn;
}

- (UIButton *)goLoginBtn{
    if (!_goLoginBtn) {
        _goLoginBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        _goLoginBtn.backgroundColor = [UIColor clearColor];
        [_goLoginBtn setTitle:@"去登录" forState:(UIControlStateNormal)];
        [_goLoginBtn setTitleColor:RGB(56, 165, 241) forState:(UIControlStateNormal)];
    }
    return _goLoginBtn;
}
#pragma mark - 下一页按钮点击方法
- (void)goNextViewController{
    SXTLog(@"我已经活了，可以进行点击了");
    if (_newxtBlock) {
        _newxtBlock(@{@"userName":_userPhoneText.text,@"password":_passwordText.text});
    }
}
#pragma mark - textFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    if (textField == _userPhoneText && range.location == 11) {
        return NO;
    }
    return YES;
}

#pragma mark - textfield修改文本内容时调用的方法
- (void)userTextFieldChangeText:(UITextField *)textFiedl{
    
    if (textFiedl.text.length == 11 && _passwordText.text.length > 5) {
        _nextViewBtn.backgroundColor = RGB(56, 166, 241);
        _nextViewBtn.userInteractionEnabled = YES;
    }else{
        _nextViewBtn.backgroundColor = RGB(229, 229, 229);
        _nextViewBtn.userInteractionEnabled = NO;
    }
}

- (void)passwordTextChangeText:(UITextField *)textFiedl{
    
    if (textFiedl.text.length > 5 && _userPhoneText.text.length == 11) {
        _nextViewBtn.backgroundColor = RGB(56, 166, 241);
        _nextViewBtn.userInteractionEnabled = YES;
    }else{
        _nextViewBtn.backgroundColor = RGB(229, 229, 229);
        _nextViewBtn.userInteractionEnabled = NO;
    }
}
@end
